package com.cf.util;

import java.util.*;

/**
 * boot-common-CollectUtil
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/7/27 下午 04:26
 */
public class CollectUtil {

    /**
     * 更具key分组
     *
     * @param data
     * @param groupKeyName
     * @param <V>
     * @param <K>
     * @return
     */
    public static <K, V> Map<K, List<V>> groupToListInMap(List<V> data, String groupKeyName, Class<K> keyType) {
        Map<K, List<V>> map = new HashMap();
        data.stream().forEach(v -> {
            K keyValue = (K) ReflectionUtil.getFieldValue(v, groupKeyName);
            if (keyValue != null) {
                List<V> vs = map.get(keyValue);
                if (vs == null) {
                    vs = new ArrayList<>();
                    map.put(keyValue, vs);
                }
                vs.add(v);
            }
        });
        return map;
    }

    /**
     * list分组
     *
     * @param data
     * @param groupKeyName
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> Map<K, V> groupSignlInMap(List<V> data, String groupKeyName, Class<K> keyType) {
        Map<K, V> map = new HashMap();
        data.stream().forEach(v -> {
            K keyValue = (K) ReflectionUtil.getFieldValue(v, groupKeyName);
            map.putIfAbsent(keyValue, v);
        });
        return map;
    }

    /**
     * list分组
     *
     * @param data
     * @param groupKeyName
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> Map<K, V> groupSignlInMapWithoutExist(List<V> data, String groupKeyName, Class<K> keyType) {
        Map<K, V> map = new HashMap();
        data.stream().forEach(v -> {
            K keyValue = (K) ReflectionUtil.getFieldValue(v, groupKeyName);
            map.put(keyValue, v);
        });
        return map;
    }

    /**
     * 提取list中的某个值
     *
     * @param data
     * @param key
     * @param valueType
     * @param <V>
     * @return
     */
    public static <V> List<V> extra(List data, String key, Class<V> valueType) {
        List<V> extraData = new ArrayList<>();
        data.forEach(d -> {
            V fieldValue = (V) ReflectionUtil.getFieldValue(d, key);
            if (fieldValue != null)
                extraData.add(fieldValue);
        });
        return extraData;
    }

    /**
     * 抽取特定数据中符合targetValue的数据的指定值
     *
     * @param beanExtraData
     * @param filterKey
     * @param targetValue
     * @param extraKey
     * @param extraType
     * @param <V>
     * @param <ET>
     * @return
     */
    public static <V, ET> List<ET> extra(List<V> beanExtraData, String filterKey, Object targetValue, String extraKey, Class<ET> extraType) {
        List<ET> ets = new LinkedList<>();
        beanExtraData.forEach(v -> {
            Object fieldValue = ReflectionUtil.getFieldValue(v, filterKey);
            if (fieldValue == null)
                return;
            if (fieldValue.equals(targetValue)) {
                ET extraValue = (ET) ReflectionUtil.getFieldValue(v, extraKey);
                if (extraValue != null) {
                    ets.add(extraValue);
                }
            }
        });
        return ets;
    }

}
